package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.BusinessAreaBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.BusinessAreaMapper;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.ValidationKey;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 添加营业区域
 */
public class BusinessAreaAdd implements ServerInterface {

    private static Logger logger = LogManager.getLogger(BusinessAreaAdd.class);

    @Override
    @ValidationKey(clazz = BusinessAreaBean.class,method="insert")
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            BusinessAreaMapper mapper = factory.getMapper(BusinessAreaMapper.class);
            BusinessAreaBean bean = JsonUtils.objectToPojo(req.getData(), BusinessAreaBean.class);
            //if (StringUtils.isNullOrEmpty(bean.getBusinessAbodeId())){
            //    return MessageBean.create(Constant.ILLEGAL_PARAMETER, "请选择营业所", void.class);
            //}
            // 校验字段重复
            if (mapper.check("area_name", bean.getAreaName()) > 0) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "营业区域名称：" + bean.getAreaName() + "的信息已存在。", void.class);
            }
            if (mapper.check("area_no", bean.getAreaNo()) > 0) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "营业区域编号：" + bean.getAreaNo() + "的信息已存在。", void.class);
            }
            bean.setCreateInfo(userInfo);
            bean.setGroupCode(userInfo.getCurWaterCode());
            mapper.insertBusinessArea(bean);
        } catch (Exception e) {
            logger.error("添加营业区域失败", e);
            return MessageBean.create(Constant.ILLEGAL_PARAMETER, "添加营业区域失败", void.class);
        }
        return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
    }

}
